package com.mayflygeek.mayflyask.framework.orm.entity;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.hibernate.annotations.AttributeAccessor;

import com.mayflygeek.mayflyask.framework.core.Idable;

/**
 * @author 虫虫
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * @Emial chongchong@mayflygeek.com
 * 
 */
@MappedSuperclass
public class IdEntity implements Serializable, Idable<Long> {
	private static final long serialVersionUID = -4261702719655229716L;
	
	@Id
	@AttributeAccessor(value = "property")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Override
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	@Transient
	public boolean getIsNew() {
		return this.id == null;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(obj == null) {
			return false;
		}
		
		if(getClass() != obj.getClass()) {
			return false;
		}
		
		IdEntity other = (IdEntity) obj;
		if(id == null) {
			if(other.id  != null) {
				return false;
			}
		} else if(!id.equals(other.id)) {
			return false;
		}
		return true;
	}

}
